const conn = require("../../data");
const fs = require("fs");

// #region 发展历程
module.exports.DBintro = (req, res) => {
    conn.query(`select * from couse`, (error, reslut) => {
        if (error) return console.log(error);
        res.render("./background/BG_about/BGcourse", { list: reslut });
    });
}

// 删除条项
module.exports.BGdeleCourse = (req, res) => {
    conn.query("delete from couse where id =?", [req.query.id], (error, reslut) => {
        if (error) return console.log(error);
        if (reslut.affectedRows) {
            res.json({
                code: 2000,
                msg: "删除成功！"
            })
        }
    })
}
// 添加条项
module.exports.BGaddCourse = (req, res) => {
    conn.query("insert into couse values(?,?,?)", [null, req.body.year, req.body.yaeytext], (error, reslut) => {
        if (error) return console.log(error);
        if (reslut.affectedRows) {
            res.json({
                code: 2001,
                msg: "添加成功！"
            })
        } else {
            res.json({
                code: 2002,
                msg: "添加失败！"
            })
        }
    })
}
// 修改回显
module.exports.BGupdateCourse = (req, res) => {
    conn.query("select * from couse where id = ?", [req.query.id], (error, reslut) => {
        if (error) return console.log(error) ;
        res.json(reslut[0])
    })
}
// 修改
module.exports.BGupCourse = (req, res) => {
    conn.query("update couse set year = ?,`describe` = ? where id = ?", [req.body.upyear, req.body.upyear_text, req.body.id], (error, reslut) => {
        if (error) return console.log(error);
        if (reslut.affectedRows) {
            res.json({
                code: 2003,
                msg: "修改成功！"
            })
        }
    })
}
// #endregion

// #region 荣誉资历
module.exports.DBhonor = (req, res) => {
    conn.query(`select * from aboutQualifications`, (error, reslut) => {
        if (error) return console.log(error);
        res.render("./background/BG_about/BGqualifications", { list: reslut });
    });
}
// 图片上传
module.exports.BGprofilequa = (req, res) => {
    fs.writeFile('./public/page/upload/' + req.file.originalname, req.file.buffer, (error) => {
        if (error) {
            return console.log(error);
        }
        res.json({
            pic: '/page/upload/' + req.file.originalname
        })
    })
}
// 添加
module.exports.BGaddfile = (req, res) => {
    conn.query("insert into aboutQualifications values(?,?,?,?)", [null, req.body.addfile, req.body.message_text, null], (error, reslut) => {
        if (error) return console.log(error);
        if (reslut.affectedRows) {
            res.json({ code: 3001, msg: "添加成功！" })
        }
    })
}
// 删除
module.exports.BGdelequa = (req, res) => {
    conn.query("delete from aboutQualifications where id =?", [req.query.id], (error, reslut) => {
        if (error) return error;
        if (reslut.affectedRows) {
            res.json({
                code: 3000,
                msg: "删除成功！"
            })
        }
    })
}

// 修改回显
module.exports.BGupdatequa = (req, res) => {
    conn.query("select * from aboutQualifications where id = ?", [req.query.id], (error, reslut) => {
        if (error) return error;
        res.json(reslut[0])
    })
}

// 修改
module.exports.BGupqua = (req, res) => {
    conn.query("update aboutQualifications set image = ?,`introduce` = ? where id = ?", [req.body.addfile, req.body.updatefile_text, req.body.id], (error, reslut) => {
        if (error) return console.log(error);
        if (reslut.affectedRows) {
            res.json({
                code: 2003,
                msg: "修改成功！"
            })
        }
    })
}
 // #endregion

// #region  企业相册
module.exports.DBcategories = (req, res) => {
    conn.query(`select * from album`, (error, reslut) => {
        if (error) return console.log(error);
        res.render("./background/BG_about/DBcategories", { list: reslut });
    });
}
// 相片添加
module.exports.BGaddteg = (req, res) => {
    conn.query("insert into album values(?,?,?,?)", [null, req.body.addfile, req.body.message_text, null], (error, reslut) => {
        if (error) return console.log(error);
        if (reslut.affectedRows) {
            res.json({ code: 3001, msg: "添加成功！" })
        }
    })
}
// 删除相片
module.exports.BGdeleteteg = (req, res) => {
    conn.query("delete from album where id =?", [req.query.id], (error, reslut) => {
        if (error) return error;
        if (reslut.affectedRows) {
            res.json({
                code: 3000,
                msg: "删除成功！"
            })
        }
    })
}
// 修改回显
module.exports.BGupdateteg = (req, res) => {
    conn.query("select * from album where id = ?", [req.query.id], (error, reslut) => {
        if (error) return error;
        res.json(reslut[0])
    })
}
// 确认修改
module.exports.BGupteg = (req, res) => {
    conn.query("update album set image = ?,`introduce` = ? where id = ?", [req.body.addfile, req.body.updatefile_text, req.body.id], (error, reslut) => {
        if (error) return console.log(error);
        if (reslut.affectedRows) {
            res.json({
                code: 2003,
                msg: "修改成功！"
            })
        }
    })
}
 // #endregion

// #region 核心团队
module.exports.BGcategories = (req, res) => {
    conn.query(`select * from core_team_people`, (error, reslut) => {
        if (error) return console.log(error);
        res.render("./background/BG_about/BG_team", { list: reslut });
    });
}


// 添加人员
module.exports.BGaddteam = (req, res) => {
    let sqldata = [null, req.body.addfile, req.body.name, req.body.job, req.body.title1, req.body.title2, req.body.title3, req.body.title4, req.body.title5, req.body.title6, req.body.message_text, "29"];
    conn.query("insert into core_team_people values(?,?,?,?,?,?,?,?,?,?,?,?)", sqldata, (error, reslut) => {
        if (error) return console.log(error);
        if (reslut.affectedRows) {
            res.json({ code: 3001, msg: "添加成功！" })
        }
    })
}

// 删除人员
module.exports.BGdeleteam = (req, res) => {
    conn.query("delete from core_team_people where id =?", [req.query.id], (error, reslut) => {
        if (error) return error;
        if (reslut.affectedRows) {
            res.json({
                code: 3000,
                msg: "删除成功！"
            })
        }
    })
}

// 修改回显
module.exports.BGupdateteam = (req, res) => {
    conn.query("select * from core_team_people where id = ?", [req.query.id], (error, reslut) => {
        if (error) return error;
        res.json(reslut[0])
    })
}

//修改
module.exports.BGupteam = (req, res) => {
    let sqldata = [req.body.addfile, req.body.name, req.body.job, req.body.title1, req.body.title2, req.body.title3, req.body.title4, req.body.title5, req.body.title6, req.body.message_text,req.body.id];
    conn.query(`update core_team_people set site=?,name=?,job=?,title1=?,title2=?,title3=?,title4=?,title5=?,title6=?,introduce=? where id=?`, sqldata, (error, reslut) => {
        if (error) return console.log(error);
        if (reslut.affectedRows) {
            res.json({
                code: 2003,
                msg: "修改成功！"
            })
        }
    })
}
 // #endregion